//
//  CalendarDateController.m
//  iosMathExample
//
//  Created by 李群 on 2021/9/18.
//

#import "CalendarDateController.h"
#import "DateCollectionViewCell.h"
#import "DateHeaderReusableView.h"
#import "ConstantUI.h"

@interface CalendarDateController ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    NSInteger cWidth;
    NSDateComponents *cDateComponents;
}
@property (nonatomic, strong) UICollectionView *dateCollectionView;
@end

@implementation CalendarDateController
- (void)viewDidLoad {
    [super viewDidLoad];
    cWidth = WIDTH_OF_SCREEN - space * 16;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone: timeZone];
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    cDateComponents = [calendar components:calendarUnit fromDate:NSDate.date];
    
    [self.view addSubview:self.dateCollectionView];
    NSInteger year = cDateComponents.year - min_year_1970;
    [self.dateCollectionView setContentOffset:CGPointMake(0, 4 * (space + cWidth) * year) animated:NO];
}

static CGFloat const space = 16;
static CGFloat const month_num = 12;
static NSInteger const min_year_1970 = 1970;
static NSInteger const max_year_2070 = 2070;
static CGFloat const headerHeight = 40;
- (UICollectionView *)dateCollectionView{
    if (!_dateCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = space;
        layout.minimumInteritemSpacing = space;
        layout.headerReferenceSize = CGSizeMake(WIDTH_OF_SCREEN, headerHeight);
        _dateCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(space, NAVIGATIONBAR_HEIGHT, WIDTH_OF_SCREEN - space * 2, HEIGHT_OF_SCREEN - NAVIGATIONBAR_HEIGHT) collectionViewLayout:layout];
        _dateCollectionView.backgroundColor = [UIColor clearColor];
        _dateCollectionView.showsVerticalScrollIndicator = NO;
        _dateCollectionView.delegate = self;
        _dateCollectionView.dataSource = self;
        _dateCollectionView.scrollEnabled = NO;
        [_dateCollectionView registerClass:[DateCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([DateCollectionViewCell class])];
        [_dateCollectionView registerClass:[DateHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([DateHeaderReusableView class])];
        if (@available(iOS 11.0, *)) {
            _dateCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _dateCollectionView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(cWidth, cWidth);
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return month_num;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return max_year_2070 - min_year_1970;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        DateHeaderReusableView *headColView = (DateHeaderReusableView *)[collectionView  dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([DateHeaderReusableView class]) forIndexPath:indexPath];
        [headColView updateHeaderViewWithSection:indexPath.section];
        return headColView;
    }else{
        return nil;
    }
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DateCollectionViewCell *cell = (DateCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([DateCollectionViewCell class]) forIndexPath:indexPath];
    [cell updateCellWithIndex:indexPath.row];
    return cell;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
